import { messages, getDefaultSetOfBook } from "share/common";
import React from 'react'
import { connect } from 'react-redux'
import { Tabs,Menu,Dropdown,Icon,message } from 'antd'
const TabPane = Tabs.TabPane;
import configureStore from "stores";
import { setDefaultSetOfBook } from "actions/cache";
import BaseSubsidyComp from "containers/expense-reimbursement-control/expense-standard/subsidy-rule/base-subsidy";
import BaseStandardComp from "containers/expense-reimbursement-control/expense-standard/travel-policy/travel-standard";
import BaseReimbursementComp from "containers/expense-reimbursement-control/expense-standard/reimbursement/reimbursement"
import BaseReimbursementSubComp from "containers/expense-reimbursement-control/expense-standard/reimbursement-submission/reimbursement-submission"
import BufferSet from 'containers/expense-reimbursement-control/expense-standard/buffer-set/buffer-set';

class ExpenseStandard extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      tabs: [
        // {key: 'reimbursement', name:'报销标准'},
        {key: 'travelStandard', name: messages('setting.key1197'/*差旅标准*/)},
        {key: 'reimbursement', name: messages('expense-reimbursement-control.key4')/*报销标准*/},
        {key: 'travel', name: messages('setting.key1111'/*差补规则*/)},
        {key: 'reimbursement-submission', name: messages('expense-reimbursement-control.key116')/*报销单提交管控*/},
        // {key: 'requisition', name: '申请标准'},
        {key: 'bufferSet', name: '___预订价格浮动标准___'},
      ],
      loading: true,
      nowTab: 'travelStandard',
      setOfBooks: [],
      nowSetOfBooks: {
        id: '',
        setOfBooksName: '',
        nowTab: ''
      },
    }
  }

  //Tabs点击
  onChangeTabs = (key) => {
    let { nowSetOfBooks } = this.state;
    nowSetOfBooks.nowTab = key;
    this.setState({nowTab : key, nowSetOfBooks});
    configureStore.store.dispatch(setDefaultSetOfBook({
      setOfBooksId: nowSetOfBooks.id,
      setOfBooksName: nowSetOfBooks.setOfBooksName,
      nowTab: key,
      baseCurrencyCode: nowSetOfBooks.functionalCurrencyCode
    }));
  };


  componentDidMount(){
    let result = getDefaultSetOfBook(true);
    let nowTab = this.state.nowTab;
    let setOfBooksId = '';
    let setOfBooksName = '';
    if (result && JSON.stringify(result) !== "{}") {
      setOfBooksId = result.setOfBooksId;
      setOfBooksName = result.setOfBooksName;
      if (result.nowTab) {
        this.setState({nowTab : result.nowTab});
        nowTab = result.nowTab;
      }
    }else{
      setOfBooksId = this.props.company.setOfBooksId;
      setOfBooksName = this.props.company.setOfBooksName;
    }
    this.setState({
      loading: true,
    })
    if (this.props.tenantMode) {
      getDefaultSetOfBook().then(resp => {
        if (resp.data) {
          let nowSetOfBooks = {
            id: resp.data.defaultSetOfBooksId,
            setOfBooksName: resp.data.defaultSetOfBooksName,
            nowTab: resp.data.nowTab
          };
          this.setState({ loading:false, setOfBooks: resp.data, nowSetOfBooks });
        }
      }).catch(error => {
        message.error(messages('common.error'));
      });
    }else {
      let nowSetOfBooks = {
        id: setOfBooksId,
        setOfBooksName,
        nowTab
      };
      configureStore.store.dispatch(setDefaultSetOfBook({
        setOfBooksId: nowSetOfBooks.id,
        setOfBooksName: nowSetOfBooks.setOfBooksName,
        nowTab: nowSetOfBooks.nowTab,
        baseCurrencyCode: nowSetOfBooks.functionalCurrencyCode
      }));
      this.setState({ setOfBooks: [nowSetOfBooks], nowSetOfBooks})
    }
  }

  getStandardComponents = () => {
    const { nowTab } = this.state;
    switch (nowTab) {
      case 'travelStandard':
        return <BaseStandardComp nowSetOfBooks={this.state.nowSetOfBooks}/>;
      case 'travel':
        return <BaseSubsidyComp nowSetOfBooks={this.state.nowSetOfBooks}/>;
      case 'reimbursement':
        return <BaseReimbursementComp nowSetOfBooks={this.state.nowSetOfBooks}/>;
      case 'reimbursement-submission':
        return <BaseReimbursementSubComp nowSetOfBooks={this.state.nowSetOfBooks}/>;
      case 'bufferSet':
        return <BufferSet/>;
    }
  };

  renderTabs() {
    return (
      this.state.tabs.map(tab => {
        return <TabPane tab={tab.name} key={tab.key}/>
      })
    )
  }

  handleChangeSetOfBooks = (e) => {
    const { setOfBooks } = this.state;
    let nowSetOfBooks = {};
    setOfBooks.map(item => {
      if(item.id === e.key)
        nowSetOfBooks = item;
    });
    nowSetOfBooks.nowTab = this.state.nowTab;
    configureStore.store.dispatch(setDefaultSetOfBook({
      setOfBooksId: nowSetOfBooks.id,
      setOfBooksName: nowSetOfBooks.setOfBooksName,
      nowTab: nowSetOfBooks.nowTab,
      baseCurrencyCode: nowSetOfBooks.functionalCurrencyCode
    }));
    this.setState({ nowSetOfBooks })
  };

  render() {
    const { nowTab, loading, setOfBooks,nowSetOfBooks } = this.state;
    const {tenantMode} = this.props;
    const menuSetOfBooks = (
      <Menu onClick={this.handleChangeSetOfBooks}>
        {setOfBooks.map(item => <Menu.Item key={item.id}>{item.setOfBooksName}</Menu.Item>)}
      </Menu>
    );
    return (
      <div>
        {this.props.tenantMode && (
          <div style={{marginBottom: 20}}>
            <span>{messages('setting.key503'/*帐套*/)}：</span>
            <Dropdown overlay={menuSetOfBooks}>
              <span className='default-color-font'>{nowSetOfBooks.setOfBooksName} <Icon type="down"/></span>
            </Dropdown>
          </div>
        )}

        {this.state.nowSetOfBooks &&
        (<Tabs type="card" onChange={this.onChangeTabs} activeKey={nowTab}>
          {this.renderTabs()}
        </Tabs>)}
        {!loading &&tenantMode&& this.state.nowSetOfBooks && this.getStandardComponents()}
        {!tenantMode && this.getStandardComponents()}
      </div>
    )
  }
}

ExpenseStandard.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    company: state.login.company,
    tenantMode: state.main.tenantMode
  }
}

export default connect(mapStateToProps)(ExpenseStandard)
